package com.example.testing.myphonestatelistener;


         import android.app.Notification;
         import android.app.NotificationManager;
         import android.app.Service;
         import android.bluetooth.BluetoothClass;
         import android.content.BroadcastReceiver;
         import android.content.Context;
         import android.content.Intent;
         import android.support.v7.app.NotificationCompat;
         import android.telephony.TelephonyManager;
       import android.util.Log;

         import static android.content.Context.NOTIFICATION_SERVICE;


public class PhoneBroadcastReceiver extends BroadcastReceiver {


     private static final String TAG="liuhunan";
     private static boolean incomingFlag=false;
     private static String incomingNumber=null;


         @Override
    public void onReceive(Context context, Intent intent) {

             //如果是去电
            if(intent.getAction().equals(intent.ACTION_NEW_OUTGOING_CALL)){
//                NotificationCompat.Builder builder=new NotificationCompat.Builde(this)
//                        .setSmallIcon(R.mipmap.itel)
//                        .setContentTitle("通话状态")
//                        .setContentText("现在的通话状态为来电ing");
//                Notification notification=builder.build();
//                NotificationManager notificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
                 incomingFlag=false;
                String callNumber=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                    Log.i(TAG,"是否来电："+ incomingFlag+" 通话状态为呼出, "+" 号码为： "+callNumber );   }
        //如果是来电
             else{
                   TelephonyManager telephonyManager= (TelephonyManager) context.getSystemService
                          (Service.TELEPHONY_SERVICE);


                   switch (telephonyManager.getCallState()){
                            case TelephonyManager.CALL_STATE_RINGING:
                                   incomingFlag=true;
                                  incomingNumber=intent.getStringExtra("incoming_number");
                                   Log.i(TAG,"是否来电： "+ incomingFlag+" 通话状态为来电ing "+" 来电号码为： "+incomingNumber );



                                   break;
                           case TelephonyManager.CALL_STATE_OFFHOOK:
                                if(incomingFlag){


                                           Log.i(TAG,"是否来电： "+ incomingFlag+" 通话状态为通话ing "+" 通话号码为： "+incomingNumber );
                              }


                                     break;
                             case TelephonyManager.CALL_STATE_IDLE:
                                  Log.i(TAG,"是否来电： "+ incomingFlag+" 通话状态为空闲状态 "+" 号码为： "+null );
                                   break;


                         }
                 }
           }


         }
